package gui.standard.form;

import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.tablemodel.ArtikalTableModel;
import net.miginfocom.swing.MigLayout;
import util.components.JDecimalTextField;
import util.components.JDigitsTextField;
import util.components.JLimitedTextField;
import actions.standard.form.ZoomFormAction;

public class ArtikalForm extends StandardForm {

	private static final long serialVersionUID = 1L;
	private JLimitedTextField tfSifraGrupe;
	private JDigitsTextField tfSifraArtikla;
	private JDecimalTextField tfPakovanje;
	private JLimitedTextField tfJediniceMere;
	private JLimitedTextField tfNazivArtikla;

	private JButton bGrupaZoom;

	

	public ArtikalForm() {

	}
	
	@Override
	public void init(){
		
		super.init();
		
		setTitle("Artikal");
		
		tableModel = new ArtikalTableModel(new String[] {
				"Sifra grupe", "Sifra artikla", "Pakovanje", "Jedinice mere",
				"Naziv artikla" }, 0);

		tblGrid.setModel(tableModel);
		
		tfSifraGrupe = new JLimitedTextField(12, 12, true);
		tfSifraArtikla = new JDigitsTextField(12, 12, true, true);
		tfPakovanje = new JDecimalTextField(10, 9, 2, ',', true);
		tfJediniceMere = new JLimitedTextField(5, 5, true);
		tfNazivArtikla = new JLimitedTextField(30, 30, true);
		
		bGrupaZoom = new JButton(new ZoomFormAction(this,
				new GrupaArtikalaForm()));
		
		children.add(new MagacinskaKarticaForm());
		children.add(new StavkaPopisaForm());
		children.add(new StavkaPrometnogDokumentaForm());
		
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new MigLayout("fillx"));
		JPanel dataPanel = new JPanel();
		dataPanel.setLayout(new MigLayout("gapx 15px"));

		JLabel lblSifraGrupe = new JLabel("Sifra grupe:");
		JLabel lblSifraArtikla = new JLabel("Sifra artikla:");
		JLabel lblPakovanje = new JLabel("Pakovanje:");
		JLabel lblJediniceMere = new JLabel("Jedinice mere:");
		JLabel lblNazivArtikla = new JLabel("Naziv artikla:");

		dataPanel.add(lblSifraGrupe);
		dataPanel.add(tfSifraGrupe);
		dataPanel.add(bGrupaZoom, "wrap");
		dataPanel.add(lblSifraArtikla);
		dataPanel.add(tfSifraArtikla, "pushx");
		dataPanel.add(lblPakovanje);
		dataPanel.add(tfPakovanje, "wrap");
		dataPanel.add(lblJediniceMere);
		dataPanel.add(tfJediniceMere, "pushx");
		dataPanel.add(lblNazivArtikla);
		dataPanel.add(tfNazivArtikla, "wrap");
		bottomPanel.add(dataPanel);

		bottomPanel.add(buttonsPanel, "dock east");

		add(bottomPanel, "grow, wrap");
	}

	@Override
	public void sync() {
		// TODO Auto-generated method stub
		int index = tblGrid.getSelectedRow();
		if (index < 0) {
			tfSifraGrupe.setText("");
			tfSifraArtikla.setText("");
			tfPakovanje.setText("");
			tfJediniceMere.setText("");
			tfNazivArtikla.setText("");
			return;
		}

		String sifraGrupe = (String) tableModel.getValueAt(index, 0);
		String sifraArtikla = (String) tableModel.getValueAt(index, 1);
		String pakovanje = (String) tableModel.getValueAt(index, 2);
		String jediniceMere = (String) tableModel.getValueAt(index, 3);
		String nazivArtikla = (String) tableModel.getValueAt(index, 4);
		
		pakovanje = pakovanje.replace(",", ".");
		
		double dPakovanje = Double.parseDouble(pakovanje);

		tfSifraGrupe.setText(sifraGrupe);
		tfSifraArtikla.setText(sifraArtikla);
		tfPakovanje.setValue(dPakovanje);
		tfJediniceMere.setText(jediniceMere);
		tfNazivArtikla.setText(nazivArtikla);

		setMode(StandardForm.MODE_EDIT);

	}

	@Override
	public void addRow() {
		// TODO Auto-generated method stub
		String sifraGrupe = tfSifraGrupe.getText().trim();
		String sifraArtikla = tfSifraArtikla.getText().trim();
		String pakovanje = Double.toString(tfPakovanje.getValue());
		String jediniceMere = tfJediniceMere.getText().trim();
		String nazivArtikla = tfNazivArtikla.getText().trim();
		try {
			int index = tableModel.insertRow(new String[] { sifraGrupe,
					sifraArtikla, pakovanje, jediniceMere, nazivArtikla });
			tblGrid.setRowSelectionInterval(index, index);
			setMode(MODE_ADD);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void searchRow() {
		// TODO Auto-generated method stub
		String sifraGrupe = tfSifraGrupe.getText().trim();
		String sifraArtikla = tfSifraArtikla.getText().trim();
		String pakovanje = Double.toString(tfPakovanje.getValue());
		String jediniceMere = tfJediniceMere.getText().trim();
		String nazivArtikla = tfNazivArtikla.getText().trim();
		try {
			tableModel.searchRows(new String[] { sifraGrupe, sifraArtikla,
					pakovanje, jediniceMere, nazivArtikla });
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void editRow() {
		// TODO Auto-generated method stub
		String sifraGrupe = tfSifraGrupe.getText().trim();
		String sifraArtikla = tfSifraArtikla.getText().trim();
		String pakovanje = Double.toString(tfPakovanje.getValue());
		String jediniceMere = tfJediniceMere.getText().trim();
		String nazivArtikla = tfNazivArtikla.getText().trim();
		try {
			int index = tableModel.editRow(new String[] { sifraGrupe,
					sifraArtikla, pakovanje, jediniceMere, nazivArtikla },
					tblGrid.getSelectedRow());
			tblGrid.setRowSelectionInterval(index, index);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void setMode(int mode) {
		super.setMode(mode);

		if (mode != StandardForm.MODE_ADD)
			return;

		tfSifraGrupe.setText("");
		tfSifraArtikla.setText("");
		tfPakovanje.setText("");
		tfJediniceMere.setText("");
		tfNazivArtikla.setText("");
	}

	@Override
	public String[] getSelectedRowKeys() {
		// TODO Auto-generated method stub
		return new String[]{tfSifraGrupe.getText().trim(), tfSifraArtikla.getText().trim()};
	}

	@Override
	public void getZoomedCol(String[] keys, StandardForm form) {
		// TODO Auto-generated method stub
		if(form instanceof GrupaArtikalaForm)
			tfSifraGrupe.setText(keys[0]);
	}

	@Override
	public String[] getSelectedRowNextKeys(int index) {
		// TODO Auto-generated method stub
		String[] retval = new String[]{};
		
		switch(index){
		case 0:{
			retval = new String[]{"", "", "", tfSifraGrupe.getText().trim(), tfSifraArtikla.getText().trim(), "", "", "", "",
					"", "", "", "", "", "", "", ""};
			break;
		}
		case 1:{
			retval = new String[]{"", "", "", "", "", "", "", "", tfSifraGrupe.getText().trim(), tfSifraArtikla.getText().trim(),
					"", ""};
			break;
		}
		case 2:{
			retval = new String[]{"", "", "", "", "", tfSifraGrupe.getText().trim(), tfSifraArtikla.getText().trim(), 
					"", "", ""};
			break;
		}
		}
		
		return retval;
	}

}
